/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.custom.service;

import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.custom.dao.CodeRuleDao;
import com.jeesite.modules.custom.entity.CodeRule;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 编码规则Service
 *
 * @author Kilo
 * @version 2021-01-22
 */
@Service
@Transactional(readOnly = true)
public class CodeRuleService extends CrudService<CodeRuleDao, CodeRule> {

    private static final Logger logger = LoggerFactory.getLogger(CodeRuleService.class);

    /**
     * 获取单条数据
     *
     * @param codeRule
     * @return
     */
    @Override
    public CodeRule get(CodeRule codeRule) {
        return super.get(codeRule);
    }

    /**
     * 查询分页数据
     *
     * @param codeRule      查询条件
     * @param codeRule.page 分页对象
     * @return
     */
    @Override
    public Page<CodeRule> findPage(CodeRule codeRule) {
        return super.findPage(codeRule);
    }

    /**
     * 保存数据（插入或更新）
     *
     * @param codeRule
     */
    @Override
    @Transactional(readOnly = false)
    public void save(CodeRule codeRule) {
        super.save(codeRule);
    }

    /**
     * 更新状态
     *
     * @param codeRule
     */
    @Override
    @Transactional(readOnly = false)
    public void updateStatus(CodeRule codeRule) {
        super.updateStatus(codeRule);
    }

    /**
     * 删除数据
     *
     * @param codeRule
     */
    @Override
    @Transactional(readOnly = false)
    public void delete(CodeRule codeRule) {
        super.delete(codeRule);
    }

    /**
     * 获取编号
     *
     * @param recordType
     * @return
     */
    public String getCode(String recordType) {
        String code = "";
        CodeRule codeRule = new CodeRule();
        codeRule.setRecordType(recordType);
        List<CodeRule> list = super.findList(codeRule);
        if (CollectionUtils.isEmpty(list)) {
            return code;
        }
        CodeRule rule = list.get(0);
        String prefix = rule.getPrefix();
        Long numberLength = rule.getNumberLength();
        Long number = 0L;
        if (rule.getCurrentNumber() != 0) {
            number = rule.getCurrentNumber() + 1;
        } else {
            number = rule.getInitNumber();
        }
        String currentNumber = String.format("%0" + numberLength + "d", number);
        String yearFlag = rule.getYearFlag();
        if ("1".equals(yearFlag)) {
            String year = DateUtils.getYear();
            String month = DateUtils.getMonth();
            code = prefix + year.substring(year.length()-2) + month + currentNumber;
        } else {
            code = prefix + currentNumber;
        }
        return code;
    }

    /**
     * 更新当前编码
     *
     * @param recordType
     */
    @Transactional(readOnly = false)
    public void updateCurrentNumber(String recordType) {
        CodeRule codeRule = new CodeRule();
        codeRule.setRecordType(recordType);
        Lock lock = new ReentrantLock();
        try {
            lock.lock();
            CodeRule rule = super.findList(codeRule).get(0);
            rule.setCurrentNumber(rule.getCurrentNumber() + 1);
            super.save(rule);
        } catch (Exception e) {
            logger.error("更新编码规则异常");
        } finally {
            lock.unlock();
        }
    }
}